package cmd

import (
	"lark/conf"
	"lark/helper"
	"time"

	"github.com/spf13/cobra"
)

var daemon bool = true

func init() {
	startFlags := StartCmd.PersistentFlags()
	//dove
	startFlags.StringVar(&conf.DoveClientAddr, "dove_client_addr", "unix:///var/lib/doveclient/doveclient.sock", "连接 doveclient 的地址")
	startFlags.DurationVar(&conf.DoveClientDialTimeout, "dove_client_dial_addr", time.Second, "连接 doveclient 超时时间")
	startFlags.DurationVar(&conf.ReadDoveClientTimeout, "dove_client_read_timeout", time.Second, "读 doveclient 超时时间")
	startFlags.DurationVar(&conf.WriteDoveClientTimeout, "dove_client_write_timeout", time.Second, "写 doveclient 超时时间")

	//default net
	startFlags.Var(helper.PflagTimeValue(conf.SetDefaultNetReadTimeout, time.Second*20), "default_net_read_timeout", "默认网络读超时")
	startFlags.Var(helper.PflagTimeValue(conf.SetDefaultNetWriteTimeout, time.Second*10), "default_net_write_timeout", "默认网络写超时")
	startFlags.Var(helper.PflagTimeValue(conf.SetDefaultNetDialTimeout, time.Second), "default_net_dial_timeout", "默认网络连接超时")

	//client net
	startFlags.StringVar(&conf.ClientListenStr, "client_listen_str", "tcp://127.0.0.1:12311", "lark client 监听地址")
	startFlags.Var(helper.PflagTimeValue(conf.SetClientAgentDialRemoteTimeout, time.Second), "client_agent_dial_remote_timeout", "lark client 连接对端网络超时")
	startFlags.Var(helper.PflagTimeValue(conf.SetClientAgentReadRemoteTimeout, time.Hour*2), "client_agent_read_remote_timeout", "lark client 读对端网络数据超时时间")
	startFlags.Var(helper.PflagTimeValue(conf.SetClientAgentWriteRemoteTimeout, time.Second*3), "client_agent_write_remote_timeout", "lark client 写对端网络超时时间")
	startFlags.Var(helper.PflagTimeValue(conf.SetClientAgentReadLocalTimeout, time.Second*20), "client_agent_read_local_timeout", "lark client 读取本地网络数据超时时间")
	startFlags.Var(helper.PflagTimeValue(conf.SetClientAgentWriteLocalTimeout, time.Second*3), "client_agent_write_local_timeout", "lark client  写本地网络超时时间")

	//client log
	startFlags.StringVar(&conf.ClientAgentLogFileName, "client_agent_log_filename", "/home/logs/lark/lark_client.log", "lark client 日志文件名")
	startFlags.StringVar(&conf.ClientAgentLogLevel, "client_agent_log_level", "error", "lark client 日志等级")
	startFlags.IntVar(&conf.ClientAgentLogMaxSize, "client_agent_log_max_size", 1024*1024*1024, "lark client 日志文件切割大小")
	startFlags.BoolVar(&conf.ClientAgentLogDaily, "client_agent_log_daily", true, "lark client 日志是否每日切割")
	startFlags.IntVar(&conf.ClientAgentLogMaxDays, "client_agent_log_max_days", 5, "lark client 日志最多保留多少天")
	startFlags.StringVar(&conf.ClientAgentLogPerm, "client_agent_log_perm", "0644", "lark client 日志文件权限")
	startFlags.StringVar(&conf.ClientAgentLogRotatePerm, "client_agent_log_rotate_perm", "0444", "lark client 日志切割之后的权限")

	//client other
	startFlags.Var(helper.PflagTimeValue(conf.SetClientAgentProcessTimeout, time.Second*20), "client_agent_process_timeout", "client agent 处理请求来自本地的请求的超时时间")

	//server net
	startFlags.StringVar(&conf.ServerListenStr, "server_listen_str", "tcp://127.0.0.1:12312", "lark server 监听地址")
	startFlags.Var(helper.PflagTimeValue(conf.SetServerAgentReadRemoteTimeout, time.Hour*3), "server_agent_read_remote_timeout", "lark server 读对端网络数据超时时间")
	startFlags.Var(helper.PflagTimeValue(conf.SetServerAgentWriteRemoteTimeout, time.Second*3), "server_agent_write_remote_timeout", "lark server 写对端网络超时时间")
	startFlags.Var(helper.PflagTimeValue(conf.SetServerAgentDialLocalTimeout, time.Second*3), "server_agent_dial_local_timeout", "lark server 连接本地网络超时")
	startFlags.Var(helper.PflagTimeValue(conf.SetServerAgentReadLocalTimeout, time.Second*20), "server_agent_read_local_timeout", "lark server 读本地网络数据超时时间")
	startFlags.Var(helper.PflagTimeValue(conf.SetServerAgentWriteLocalTimeout, time.Second*3), "server_agent_write_local_timeout", "lark server  写本地网络超时时间")

	//server log
	startFlags.StringVar(&conf.ServerAgentLogFileName, "server_agent_log_filename", "/home/logs/lark/lark_server.log", "lark server 日志文件名")
	startFlags.StringVar(&conf.ServerAgentLogLevel, "server_agent_log_level", "error", "lark server 日志等级")
	startFlags.IntVar(&conf.ServerAgentLogMaxSize, "server_agent_log_max_size", 1024*1024*1024, "lark server 日志文件切割大小")
	startFlags.BoolVar(&conf.ServerAgentLogDaily, "server_agent_log_daily", true, "lark server 日志是否每日切割")
	startFlags.IntVar(&conf.ServerAgentLogMaxDays, "server_agent_log_max_days", 5, "lark server 日志最多保留多少天")
	startFlags.StringVar(&conf.ServerAgentLogPerm, "server_agent_log_perm", "0644", "lark server 日志文件权限")
	startFlags.StringVar(&conf.ServerAgentLogRotatePerm, "server_agent_log_rotate_perm", "0444", "lark server 日志切割之后的权限")

	//server other
	startFlags.Var(helper.PflagTimeValue(conf.SetServerAgentRequestMaxWaitingTime, time.Second*2), "server_agent_request_max_waiting_time", "收到请求的最大等待时间")
	startFlags.IntVar(&conf.ServerAgentRequestBacklog, "server_agent_request_backlog", 10000, "请求最大排队数")
	startFlags.DurationVar(&conf.ServerAgentToBusinessMaxIdleTime, "server_agent_to_business_max_idle_time", time.Minute*10, "lark server 到业务连接的最大空闲时间")

	//other
	startFlags.Uint64Var(&conf.OSMaxOpenFiles, "os_max_open_files", 65535, "可以使用的最大文件句柄数")
	startFlags.StringVar(&conf.HTTPAdminAddr, "http_admin_addr", "0.0.0.0:12313", "HTTP管理入口监听地址")
	startFlags.StringVar(&conf.ServiceSnapshotPath, "service_snapshot_path", "/var/lib/lark/service_snapshot/", "服务快照保存目录")
	startFlags.StringSliceVar(&conf.EtcdEndpoints, "etcd_endpoints", []string{}, "etcd 集群的地址，逗号分隔")
	startFlags.StringSliceVar(&conf.BackupEtcdEndpoints, "backup_etcd_endpoints", []string{}, "备用etcd 集群的地址，逗号分隔")
	startFlags.StringSliceVar(&conf.AdminWhitelist, "admin_whitelist", []string{}, "管理端口ip 白名单，逗号分隔")
	startFlags.Uint32Var(&conf.MaxSizeOfNetPackage, "max_size_of_net_package", 1024*1024*200, "每个网络包的最大尺寸")

	//root
	RootCmd.PersistentFlags().BoolVar(&daemon, "daemon", true, "是否是守护进程")
	RootCmd.PersistentFlags().MarkHidden("daemon")

	//version
	VersionCmd.PersistentFlags().BoolP("all", "a", false, "输出详细版本信息")

	//add command
	RootCmd.AddCommand(VersionCmd)
	RootCmd.AddCommand(StartCmd)
}

var RootCmd = &cobra.Command{
	Use:   "lark",
	Short: "jumei service mesh",
}
